package com.course.web.component.base;

import com.google.common.collect.Lists;
import com.vaadin.data.ValueProvider;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Grid;
import com.vaadin.ui.renderers.ButtonRenderer;
import org.apache.commons.lang3.StringUtils;

import java.util.Comparator;
import java.util.List;

/**
 * Created by Justin on 2017/5/24.
 */
public class BaseGrid<T> extends CustomComponent {

    private final Grid<T> grid;
    private List<T> items;
    private SelectHandler<T> handler;

    public BaseGrid(SelectHandler<T> handler) {
        this.grid = new Grid<>();
        this.items = Lists.newLinkedList();
        this.handler = handler;
        grid.setSelectionMode(Grid.SelectionMode.NONE);
        grid.addItemClickListener(event -> select(event.getItem(), event.getColumn().getId()));
        grid.setSizeFull();
        setCompositionRoot(grid);
        setSizeFull();
    }

    public BaseGrid() {
        this(null);
    }

    public void setHandler(SelectHandler<T> handler) {
        this.handler = handler;
    }

    public void setItems(List<T> items) {
        if (items != null) {
            this.items.clear();
            this.items.addAll(items);
            flush();
        }
    }

    public List<T> getItems() {
        return items;
    }

    public void sortItems(Comparator<T> comparator) {
        items.sort(comparator);
        flush();
    }

    public void appendItem(T item) {
        if (item != null) {
            items.add(item);
            flush();
        }
    }

    public void updateItem(T item) {
        if (item != null) {
            for (int i = 0; i < items.size(); i++) {
                if (items.get(i).equals(item)) {
                    items.set(i, item);
                    flush();
                    return;
                }
            }
        }
    }

    public void deleteItem(T item) {
        if (item != null) {
            items.remove(item);
            flush();
        }
    }

    public void insertItem(T item, int index) {
        if (item != null) {
            items.add(index, item);
            flush();
        }
    }

    public Grid.Column<T, ?> addColumn(ValueProvider<T, ?> provider, String caption, String id) {
        return grid.addColumn(provider).setCaption(caption).setId(id);
    }

    public Grid.Column<T, ?> addColumn(ValueProvider<T, ?> provider, String caption) {
        return grid.addColumn(provider).setCaption(caption);
    }

    public Grid.Column<T, String> addButton(ButtonHandler<T> handler, String caption) {
        return grid.addColumn(item -> caption).setCaption(caption)
                .setRenderer(new ButtonRenderer<T>(event -> handler.onClick(event.getItem())));
    }

    private void select(T item, String id) {
        if (handler == null || item == null || StringUtils.isBlank(id)) {
            return;
        }
        handler.onSelect(item, id);
    }

    private void flush() {
        grid.setItems(items);
    }

    @FunctionalInterface
    public interface SelectHandler<T> {
        void onSelect(T item, String id);
    }

    @FunctionalInterface
    public interface ButtonHandler<T> {
        void onClick(T item);
    }
}
